home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Topik / Topik - Disk 24 - Productivity (19xx)(Topik Public Domain)(PD)[WB].zip / Topik - Disk 24 - Productivity (19xx)(Topik Public Domain)(PD)[WB].adf / Calendar / month.c < prev    next >
C/C++ Source or Header  |  1990-01-31  |  4KB  |  238 lines

  1. /* month.c */
  2.  
  3. #define AMIGA 1
  4.  
  5. #ifndef AMIGA 
  6. #include <curses.h>
  7. #include <signal.h>
  8. #include <utmp.h>
  9. #endif
  10. #include "month.h"
  11.  
  12. short initialized = 0;
  13. short dhour, dminute, dsecond;
  14. extern short crow, ccol, update_schedule, updating;
  15. extern short this_month, this_day, this_year, SCHEDULE_ROW;
  16. extern struct event_rec events;
  17. extern struct mdate mdates[];
  18. char version[50];
  19.  
  20. main(argc, argv)
  21. int argc;
  22. char *argv[];
  23. {
  24.     strcpy(version, "Calander 0.0 ported by Bob Leivian");
  25.     check_args(argc, argv);
  26.     initialize();
  27.     print_screen();
  28.     start_display();
  29.     user();
  30.     terminate();
  31. }
  32.  
  33. initialize()
  34. {
  35.     int blast_out(), i;
  36. #ifndef AMIGA
  37.     signal(SIGINT, blast_out);
  38.     signal(SIGQUIT, blast_out);
  39. #endif
  40.     get_current_date();
  41.     read_schedule();
  42.  
  43.     for (i = 0; i < 12; i++) {
  44.         mdates[i].month = this_month;
  45.         mdates[i].year = this_year;
  46.     }
  47.     initscr();
  48.     SCHEDULE_ROW = (LINES < 24) ? 17 : 18;
  49.     initialized = 1;
  50.     crmode();
  51.     noecho();
  52. }
  53.  
  54. terminate()
  55. {
  56.     if (updating) {
  57.         return;
  58.     }
  59. #ifndef AMIGA
  60.     signal(SIGINT, SIG_IGN);
  61.     signal(SIGTSTP, SIG_IGN);
  62.     signal(SIGQUIT, SIG_IGN);
  63.     signal(SIGHUP, SIG_IGN);
  64. #endif
  65.     if (initialized) {
  66.         if (update_schedule) {
  67.             mvaddstr(0, 0, "updating schedule\t");
  68.         } else {
  69.             mvaddstr(0, 0, "schedule unchanged\t");
  70.         }
  71.         refresh();
  72.         if (update_schedule) {
  73.             if (write_schedule() == -1) {
  74.                 sound_bell();
  75.                 mvaddstr(0, 0, "cannot create .month");
  76.             }
  77.         }
  78.     }
  79.     move(LINES-1, 0);
  80.     clrtoeol();
  81.     refresh();
  82.     endwin();
  83.     printf("Terminating Normally\n");
  84.     exit(0);
  85. }
  86.  
  87. blast_out()
  88. {
  89.     update_schedule = 0;
  90.     terminate();
  91. }
  92.  
  93. check_args(argc, argv)
  94. int argc;
  95. char *argv[];
  96. {
  97.     short i, daemon = 0;
  98.  
  99.     for (i = 1; i < argc; i++) {
  100.         if (argv[i][0] == '-') {
  101.             switch(argv[i][1]) {
  102.             case 'd':
  103.                 daemon = i;
  104.                 break;
  105.             default:
  106.                 goto BA;
  107.             }
  108.         } else {
  109. BA:                     printf("Bad argument: %s\n", argv[i]);
  110.             terminate();
  111.         }
  112.     }
  113.     if (daemon) {
  114. #ifdef AMIGA
  115.         /* must be 'RUN' from the cli */
  116.         daemonize();
  117. #else
  118.         if (!fork()) {
  119.             signal(SIGINT, SIG_IGN);
  120.             signal(SIGQUIT, SIG_IGN);
  121.             signal(SIGTSTP, SIG_IGN);
  122.             daemonize();
  123.         }
  124. #endif
  125.         exit(0);
  126.     }
  127. }
  128.  
  129. daemonize()
  130. {
  131.     int do_nothing();
  132.     struct event_rec *eptr;
  133.     short minutes, eminutes, diff, seconds;
  134.     int i;
  135.  
  136.     printf("daemon started\n");
  137.     fflush(stdout);
  138. AGAIN:
  139.     get_current_date();
  140.     minutes = (60 * dhour) + dminute;
  141.  
  142.     seconds = (60 * (15 - (dminute % 15) - 1)) + (60 - dsecond);
  143.     if (seconds < 60) {
  144.         seconds = 900;
  145.     }
  146.  
  147. #ifndef AMIGA
  148.     signal(SIGALRM, do_nothing);
  149.     alarm(seconds);
  150.     if (!logged_in()) {
  151.         terminate();
  152.     }
  153. #endif
  154.  
  155.     read_schedule();
  156.  
  157.     eptr = events.next_event;
  158.  
  159.     while (eptr) {
  160.  
  161.         if (event_matches_date(eptr)) {
  162.             eminutes = (((short)60) * ((short)eptr->hour)) +
  163.                 ((short)eptr->minute);
  164.             diff = eminutes - minutes;
  165.             if ((diff >= 0) && (diff <= 15)) {
  166.                 remind(eptr->event_string, diff);
  167.             }
  168.         }
  169.         eptr = eptr->next_event;
  170.     }
  171. #ifdef AMIGA
  172.     /* delay is in 50th of a second (delay 10 min, check abort 10 secs) */
  173.     for (i = 0; i < (10/*min*/*6/*times/min*/); i++) {
  174.         Chk_Abort();
  175.         Delay(50L * 10L);
  176.     }
  177. #else
  178.     pause();
  179. #endif
  180.     goto AGAIN;
  181. }
  182.  
  183. logged_in()
  184. {
  185. #ifdef AMIGA 
  186. printf("logged_in called\n");
  187. #else
  188.     static struct utmp u_buf;
  189.     struct utmp t_buf;
  190.     int fd, retval = 0;
  191.     static short called_before = 0;
  192.     char *ttyname(), *tname;
  193. AGAIN:
  194.     if ((fd = open("/etc/utmp", 0)) < 0) {
  195.         return(0);
  196.     }
  197.     if (!called_before) {
  198.         tname = ttyname(0) + 5;
  199.     }
  200.     while (read(fd, &t_buf, sizeof(struct utmp)) > 0) {
  201.         if (!called_before) {
  202.             if (!strcmp(tname, t_buf.ut_line)) {
  203.                 u_buf = t_buf;
  204.                 break;
  205.             }
  206.         } else if (byte_comp(&u_buf, &t_buf, sizeof(struct utmp))) {
  207.                 close(fd);
  208.                 retval = 1;
  209.                 break;
  210.         }
  211.     }
  212.     close(fd);
  213.     if (!called_before) {
  214.         called_before = 1;
  215.         goto AGAIN;
  216.     }
  217.     return(retval);
  218. #endif
  219. }
  220.  
  221. do_nothing()
  222. {
  223. }
  224.  
  225. byte_comp(s1, s2, n)
  226. register char *s1, *s2;
  227. register int n;
  228. {
  229.     short i;
  230.  
  231.     for (i = 0; i < n; i++) {
  232.         if (*(s1++) != *(s2++)) {
  233.             return(0);
  234.         }
  235.     }
  236.     return(1);
  237. }
  238.